package com.seguriapp.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.seguriapp.R;
import com.seguriapp.controllers.ComplaintController;
import com.seguriapp.controllers.EntityController;
import com.seguriapp.dao.CrimeCategoryDao;
import com.seguriapp.dialogs.SelectCategoryDialog;
import com.seguriapp.entities.CrimeCategoryEntity;

public class SelectCategoryListAdapter extends SimpleCursorAdapter {

	private Context mContext;
	private int mFlag;

	@SuppressWarnings("deprecation")
	public SelectCategoryListAdapter(final Context context, final int layout,
			final Cursor cursor, final String[] arrFrom, final int[] arrTo,
			final int flag) {
		super(context, layout, cursor, arrFrom, arrTo);
		mContext = context;
		mFlag = flag;
	}

	/**
	 * Obtiene el numero total de items a mostrar
	 */
	@Override
	public int getCount() {
		return getCursor().getCount();
	}

	@Override
	public Object getItem(int position) {

		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);
		final CrimeCategoryEntity category = new CrimeCategoryEntity();
		category.setName(cursor.getString(cursor
				.getColumnIndexOrThrow(CrimeCategoryDao.COL_NAME)));
		category.setSelected(cursor.getInt(cursor
				.getColumnIndexOrThrow(CrimeCategoryDao.COL_SELECTED)) > 0);
		category.setIdCategory(cursor.getInt(cursor
				.getColumnIndexOrThrow(CrimeCategoryDao.COL_CID)));
		return category;
	}

	/**
	 * Obtiene el view del nuevo item para mostrarlo en pantalla
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {

		final LinearLayout viewRelative = (LinearLayout) super.getView(
				position, convertView, parent);

		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		final TextView twTitle = (TextView) viewRelative
				.findViewById(R.id.nombreCategoria);
		final CheckBox chkBox = (CheckBox) viewRelative
				.findViewById(R.id.seleccionCategoria);

		twTitle.setText(cursor.getString(cursor
				.getColumnIndexOrThrow(CrimeCategoryDao.COL_NAME)));
		chkBox.setChecked(cursor.getInt(cursor
				.getColumnIndexOrThrow(CrimeCategoryDao.COL_SELECTED)) > 0);

		chkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final CrimeCategoryEntity category = (CrimeCategoryEntity) getItem(position);
				category.setSelected(!category.isSelected());
				if (mFlag == SelectCategoryDialog.CRIME_CATEGORY) {
					final ComplaintController cController = new ComplaintController(
							mContext);
					cController.updateSelectedCategory(category);
				} else {
					final EntityController eController = new EntityController(
							mContext);
					eController.updateSelectedCategory(category);
				}

			}
		});

		return viewRelative;
	}

	/**
	 * Set all items disabled
	 */
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	/**
	 * Metodo usado cuando el adaptador es notificado que la data en el cursor
	 * ha cambiado
	 */
	@Override
	protected void onContentChanged() {
		super.onContentChanged();
		notifyDataSetChanged();
	}

}